<?php

namespace App\Format;

use App\Constant\TimeConst;
use Carbon\Carbon;
use Illuminate\Support\Str;

class BaseFormat
{
    /**
     * 获取静态地址
     * @param $url
     * @return string
     */
    public static function baseUrl($url)
    {
        if (!$url) {
            return '';
        }
        if (substr($url, 0, 4) == "http") {
            return $url;
        }
        return config('app.url') . ltrim($url, DIRECTORY_SEPARATOR);
    }

    /**
     * 获取存储路径
     * @param $url
     * @return string
     * @name:getStoragePath
     * @author: lisa
     * @datetime: 2021/11/10 20:50
     */
    public static function getStoragePath($url) {
        $path = Str::afterLast($url, 'storage/');
        return storage_path('app/'.$path);
    }

    /**
     * 存入数据库图片格式化
     * @param array $array
     * @param string $glue
     * @return string
     */
    public static function arrayToString(array $array = [], string $glue = ','): string
    {
        return implode($glue, $array);
    }

    /**
     * 取出图片格式化
     * @param string $string
     * @param string $glue
     * @return array
     */
    public static function stringToArray(string $string = '', string $glue = ','): array
    {
        if (!$string) {
            return [];
        }
        return explode($glue, $string);
    }

    /**
     * 手机号加密
     * @param $mobile
     * @return string
     */
    public static function syncMobile($mobile): string
    {
        if (!$mobile) return '';
        return substr($mobile, 0, 3) . "****" . substr($mobile, 7);
    }

    /**
     * 身份证号码加密
     * @param $idCard
     * @return string
     */
    public static function syncIdCard($idCard): string
    {
        return substr($idCard, 0, 5) . "*********" . substr($idCard, -4);
    }

    /**
     * 姓名隐藏 中间一位
     * @param string $name
     * @return array|string
     */
    public static function nameHiding(string $name = '')
    {
        if (!$name) {
            return '';
        }
        $strLen   = mb_strlen($name, 'utf-8');
        $firstStr = mb_substr($name, 0, 1, 'utf-8');
        $lastStr  = mb_substr($name, -1, 1, 'utf-8');
        if ($strLen == 1) {
            return $name . "*";
        } else {
            return $strLen == 2 ?
                $firstStr . str_repeat('*', mb_strlen($name, 'utf-8') - 1) :
                $firstStr . str_repeat("*", $strLen - 2) . $lastStr;
        }
    }


    /**
     * 格式化评论需要的时间
     * @param Carbon $dateTime
     * @return string
     */
    public static function formatTime(Carbon $dateTime): string
    {
        if($dateTime->getTimestamp() > (time() - TimeConst::TIME_ONE_MINUTE)) {
            return '刚刚';
        }
        if ($dateTime->getTimestamp() > (time() - TimeConst::TIME_ONE_HOUR)) {
            return intval((time() - $dateTime->getTimestamp()) / TimeConst::TIME_ONE_MINUTE) . '分钟前';
        } elseif ($dateTime->isToday()) {
            return '今天 ' . $dateTime->format(TimeConst::TIME_HOUR_MINUTE);
        } elseif ($dateTime->isYesterday()) {
            return '昨天 ' . $dateTime->format(TimeConst::TIME_HOUR_MINUTE);
        } else {
            return $dateTime->format(TimeConst::DATE_NOT_SECOND);
        }
    }

    /**
     * @param string $keyword
     * @return string|string[]|null
     */
    public static function formatSearchKeyword(string $keyword)
    {
        if (empty($keyword)) {
            return '';
        }
        return preg_replace("/\s+/", '%', trim($keyword));
    }

    /**
     * 对象转数组
     * 例：原对象['key1' => 'value1', 'key2' => 'value2']
     * 转化后：[['key' => 'key1', 'value' => 'value1'], ['key' => 'key2', 'value' => 'value2']]
     * @param array $object
     * @return array
     */
    public static function objectToKeyValue(array $object): array
    {
        $data = [];
        foreach ($object as $key => $value) {
            $data[] = [
                'key'   => $key,
                'value' => $value
            ];
        }
        return $data;
    }

    /**
     * @param string $date
     * @return string
     */
    public static function formatDate($date = ''): string
    {
        if (in_array($date, [TimeConst::DEFAULT_DATE_TIME, TimeConst::DEFAULT_DATE])) return '';
        if ($date) {
            return Carbon::parse($date)->toDateString();
        } else {
            return '';
        }
    }

    /**
     * @param string $dateTime
     * @return string
     */
    public static function formatDateTime($dateTime = ''): string
    {
        if (in_array($dateTime, [TimeConst::DEFAULT_DATE_TIME, TimeConst::DEFAULT_DATE])) return '';
        if ($dateTime) {
            return Carbon::parse($dateTime)->toDateTimeString();
        } else {
            return '';
        }
    }
}
